/********************************************************************************
 *    Copyright (C) 2019 Quark Matter Research Center, IMP,CAS			*
 *                                                                              *
 *              This software is distributed under the terms of the             *
 *              GNU Lesser General Public Licence (LGPL) version 3,             *
 *                  copied verbatim in the file "LICENSE"                       *
 ********************************************************************************/
// in root all sizes are given in cm




#include "TSystem.h"
#include "TGeoManager.h"
#include "TGeoVolume.h"
#include "TGeoMaterial.h"
#include "TGeoMedium.h"
#include "TGeoPgon.h"
#include "TGeoMatrix.h"
#include "TGeoCompositeShape.h"
#include "TFile.h"
#include "TString.h"
#include "TList.h"
#include "TROOT.h"



#include <iostream>

using namespace TMath;



// some global variables
TGeoManager* gGeoMan = NULL;  // Pointer to TGeoManager instance
const double cm=1.;
const double mm=0.1;
const double strip_width=4.*cm;
const double strip_thickness=1.*cm;
const double strip_length=120.*cm;
const double gap_distance=1*mm;


void Create_tofw_v1()
{
	  // -------   Load media from media file   -----------------------------------
	FairGeoLoader*    geoLoad = new FairGeoLoader("TGeo","FairGeoLoader");
	FairGeoInterface* geoFace = geoLoad->getGeoInterface();
	TString geoPath = gSystem->Getenv("VMCWORKDIR");
	TString medFile = geoPath + "/geometry/media.geo";
	// TString medFile = geoPath + "/geometry/media_etf.geo";
	cout<<"media file is: "<<medFile<<endl;
	geoFace->setMediaFile(medFile);
	geoFace->readMedia();
	
	//Get and create the required materials which defined in the global media.geo file
	FairGeoMedia*   geoMedia = geoFace->getMedia();
	FairGeoBuilder* geoBuild = geoLoad->getGeoBuilder();
	FairGeoMedium* air       = geoMedia->getMedium("air");
	FairGeoMedium* FscScint= geoMedia->getMedium("FscScint");
	//check if all media are found
	geoBuild->createMedium(air);
	geoBuild->createMedium(FscScint);
	// -----------------------------------------------------------------------------
	

	
	// -------   Output Geometry file name  ----------------------------------------
	const TString detectorName= "tofw";
	const TString versionName = "_v1";
	// const TString FileName =detectorName + versionName + ".root";
	const TString FileName =geoPath+"/geometry/"+detectorName + versionName + ".root";
	const TString fname=detectorName + versionName;
	const TString FileName1 =geoPath+"/geometry/"+detectorName +"_geomanager"+ versionName + ".root";
	// -----------------------------------------------------------------------------


	// -------     Create geometry and top volume     ------------------------------
	gGeoMan=(TGeoManager*)gROOT->FindObject("FAIRGeom");
	gGeoMan->SetName("TOFWgeom");
	gGeoMan->SetVisLevel(7);
	TGeoVolume* top= new TGeoVolumeAssembly("TOP");
	gGeoMan->SetTopVolume(top);
	
	TGeoMedium* pMedAir = gGeoMan->GetMedium("air");
	  if ( ! pMedAir ) Fatal("Main", "Medium air not found");
	TGeoMedium* pMedTOF = gGeoMan->GetMedium("FscScint");
	  if ( ! pMedTOF ) Fatal("Main", "Medium FscScint not found");
	
	TGeoVolume* tofw= new TGeoVolumeAssembly(fname);
	
	// -------     TOFW  geometry       -------     
	// shape: TGeoBBox 
	TGeoShape *pTOFW_module= new TGeoBBox("TOFW_module_box", strip_width/2,strip_length/2,strip_thickness/2);
	TGeoVolume* tofw_module= new TGeoVolume("TOFW_module",pTOFW_module, pMedTOF);
	tofw_module->SetLineColor(kGreen);
	tofw_module->SetVisLeaves(kTRUE);
	
	//place tofw modules
	double xx;
	double yy=0;
	double zz=0;
	TGeoRotation *zeroRotation= new TGeoRotation();
	zeroRotation->RotateX(0.);
	zeroRotation->RotateY(0.);
	zeroRotation->RotateZ(0.);
	for(int i=0;i<32;i++)
	{
		xx=(i-16)*(strip_width+gap_distance)+(strip_width+gap_distance)/2;
	tofw->AddNode(tofw_module,i,new TGeoCombiTrans(xx,yy,zz,zeroRotation));
	}


	//position: dx, dy, dz
	// Double_t dx,dy,dz;
	
	double dx=190.;
	double dy = 0.;
	double dz = 700.;
	// Rotation: 
	TGeoRotation *gRot = new TGeoRotation();
	gRot->RotateX(0.);
	gRot->RotateY(15.0);
	gRot->RotateZ(0.);
	// TRANSFORMATION MATRICES
	TGeoCombiTrans* ptrans= new TGeoCombiTrans("", dx,dy,dz,gRot);  //this
	top->AddNode(tofw,1,ptrans);
	cout<<"Voxelizing."<<endl;
	top->Voxelize("");
	gGeoMan->CloseGeometry();
	tofw->Draw();
	gGeoMan->CheckOverlaps(0.001);
	gGeoMan->PrintOverlaps();
	gGeoMan->Test();

	TFile* outfile = TFile::Open(FileName,"RECREATE");
	top->Write();
	outfile->Close();
	
	TFile* outfile1 = TFile::Open(FileName1,"RECREATE");
	gGeoMan->Write();
	outfile1->Close();

}

